package co.ntt.spms.conf;

import java.io.File;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Locale;

import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;

public class ClientConfig {
    
    private static final String LOCALMAILBOX_REGPATH = "Software\\idsTrust\\WiseOne\\LocalMailBox";
    
    private static final String REGKEY_LOCALMAILBOXHOME = "LOCALMAILBOX_HOME";
    
    private static final String REGKEY_VERSION = "VERSION";
    
    private static final String REGKEY_COMPANYINDEX = "COMPANYINDEX";
    
    private static final String REGKEY_LOCALESTRING = "LOCALESTRING";
    
    private static final String REGKEY_LOGIN_ID = "LOGIN_ID";
    
    private static final String REGKEY_AUTHENTICATED = "AUTHENTICATED";
    
    private static final String REGKEY_MODE = "MODE";
    
    private static RegistryKey localBoxRegistryKey;
    
    public static final String VERSION = "v1.0.090609";
    
    static {
        try {
            localBoxRegistryKey = Registry.HKEY_LOCAL_MACHINE.createSubKey(LOCALMAILBOX_REGPATH, "", RegistryKey.ACCESS_ALL);
        }
        catch (RegistryException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 
     * @param isOnline
     */
    public static void setMode(boolean isOnline) {
    	setRegistryStr(REGKEY_MODE, isOnline ? "1" : "0");
    }
    
    /**
     * 
     * @return
     */
    public static String getMode() {
    	return getRegistryStr(REGKEY_MODE);
    }
    
    /**
     * 
     * @return
     */
    public static boolean isOnline() {
    	return (getMode() != null && getMode().equals("1")) ? true : false;
    }
    
    /**
     * �?��?????�렉?�리�?반환?�니??
     * @return
     */
    public static File getHomeDirectory() {
        File homeDirectory = new File(getHomeDirectoryPath());
        if (!homeDirectory.exists())
            homeDirectory.mkdirs();
        return homeDirectory;
    }
    
    /**
     * �?��?????�렉?�리 경로�?반환?�니??
     * @return
     */
    public static String getHomeDirectoryPath() {
        String path = getRegistryStr(REGKEY_LOCALMAILBOXHOME);
        return path;
    }
    
    /**
     * ???�렉?�리 경로�??�정?�니??
     * @param path
     */
    public static void setHomeDirectoryPath(String path) {
        setRegistryStr(REGKEY_LOCALMAILBOXHOME, path);
    }
    
    /**
     * 
     */
    public static void setAuthenticated() {
    	setRegistryStr(REGKEY_AUTHENTICATED, "1");
    }
    
    /**
     * 
     * @return
     */
    public static String getAuthenticated() {
    	return getRegistryStr(REGKEY_AUTHENTICATED, null);
    }
    
    /**
     * 로그?????�사 ?�덱?��? ?�정?�니??
     * @param index
     */
    public static void setComapanyIndex(int index) {
        setRegistryStr(REGKEY_COMPANYINDEX, "" + index, null);
    }
    
    /**
     * 로그?????�사 ?�덱?��? 조회?�니??
     * @return
     */
    public static int getCompanyIndex() {
        String index = getRegistryStr(REGKEY_COMPANYINDEX, null);
        try {
            return Integer.parseInt(index);
        } catch (Exception e) {
            return 0;   
        }
    }

    /**
     * 로그????로�????�덱?��? ?�정?�니??
     * @param index
     */
    public static void setLocaleString(String locale) {
        setRegistryStr(REGKEY_LOCALESTRING, locale, null);
    }
    
    /**
     * 로그?�시 로�??�을 조회?�다.
     * @return
     */
    public static String getLocaleString() {
    	String localeString = getRegistryStr(REGKEY_LOCALESTRING, null);
        return localeString == null ? 
        		Locale.getDefault().getLanguage().toLowerCase() + "_" + Locale.getDefault().getCountry().toUpperCase() :
        			localeString;
    }
    
    /**
     * 로�??�을 ?�려�?��.
     * @return
     */
    public static Locale getLocale() {
        String localeString = getLocaleString();
        return localeString == null ? Locale.ENGLISH : new Locale(localeString.substring(0, 2));
    }
    
    /**
     * 로그????로그???�이?��? ?�정?�니??
     * @param id
     */
    public static void setLoginId(String id) {
        setRegistryStr(REGKEY_LOGIN_ID, id, null);
    }
    
    /**
     * 로그????로그???�이?��? 조회?�니??
     * @return
     */
    public static String getLoginId() {
        return getRegistryStr(REGKEY_LOGIN_ID, null);
    }
    
    /**
     * ?��??�트리에 값을 조회?�니??
     * @param name
     * @return
     */
    private static String getRegistryStr(String name) {
        return getRegistryStr(name, "UTF-8");
    }
    
    /**
     * ?��??�트리에 값을 조회?�니??
     * @param name
     * @param encType
     * @return
     */
    private static String getRegistryStr(String name, String encType) {
        String result = null;
        try {
            result = localBoxRegistryKey.getStringValue(name);
            if (encType != null)
                result = URLDecoder.decode(localBoxRegistryKey.getStringValue(name), encType);
        }
        catch (Exception e) {
            System.out.println("Key�?찾�? 못하??��?�다.");
        }
        return result;
    }

    /**
     * ?��??�트리에 값을 ??��?�니??
     * @param name
     * @param str
     */
    private static void setRegistryStr(String name, String str) {
        setRegistryStr(name, str, "UTF-8");
    }
    
    /**
     * ?��??�트리에 값을 ??��?�니??
     * @param name
     * @param str
     * @param encType
     */
    private static void setRegistryStr(String name, String str, String encType) {
        try {
            if (encType != null)
                str = URLEncoder.encode(str, encType);
            RegStringValue value = new RegStringValue(localBoxRegistryKey, name, str);
            localBoxRegistryKey.setValue(name, value);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 버전??체크?�니??
     */
    public static void checkVersion() {
        if (getRegistryStr(REGKEY_VERSION) == null) {
            setRegistryStr(REGKEY_VERSION, VERSION);
//            encodingRegKey(REGKEY_MYDOCMENT);
            encodingRegKey(REGKEY_LOCALMAILBOXHOME);
        } 
    }
    
    /**
     * ?��??�트�??��? ?�코???�니??
     * @param name
     * @param str
     */
    private static void encodingRegKey(String name) {
        String regValue = getRegistryStr(name, null);
        if (regValue != null)
            setRegistryStr(name, regValue, "UTF-8");
    }
}
